<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <el-row>
        <el-col :span="9">
          <div class="grid-content bg-purple title_img">
            <img src="../assets/标题.png" alt width="150px" height="50px" />
            <span>电商后台管理系统</span>
          </div>
        </el-col>
        <el-col :span="15">
          <div class="grid-content bg-purple title_btn">
            <!-- 下拉菜单 -->
            <el-dropdown trigger="click" @command="handleCommand">
              <span class="el-dropdown-link">
                <img class="portrait" src="../assets/个人头像.gif" width="50px" height="50px" />
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :command="item.command" v-for="item in dropdown.list" :key="item.id">{{item.title}}</el-dropdown-item>

                <el-dropdown-item divided :command="dropdown.another.command">{{dropdown.another.title}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'" style="transition : all 0.4s">
        <div class="toggle-button" @click="toggleCollapse">{{isCollapse2}}</div>
        <el-menu background-color="#304156" text-color="#fff" class="el-menu-vertical-demo" active-text-color="#309EE1" :default-active="activePath" :collapse="isCollapse" :router="true">
          <el-menu-item index="/home" @click="saveNavState('/home')">
            <i class="el-icon-menu"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <el-submenu :index="'/'+item.path" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <el-menu-item :index="'/'+item2.path" v-for="item2 in item.children" :key="item2.id" @click="saveNavState('/'+item2.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                {{item2.authName}}
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      dropdown: {
         list: [
        {
          id: 1,
          title: '个人中心',
          command: 'personalCenter'
        },
        {
          id: 2,
          title: '首页',
          command: 'home'
        },
        {
          id: 3,
          title: '项目地址',
          command: 'giturl'
        }
      ],
      another: {
          id: 4,
          title: '退出登录',
          command: 'logout'
      }
      },
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      isCollapse2: '折叠',
      // 被激活的链接地址
      activePath: ''
    }
  },
  methods: {
    logout() {
      console.log('退出登录')
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    handleCommand(command) {
        console.log(command)
        if (command === 'logout') {
          this.logout()
        } else if (command === 'home') {
          this.$router.push('/welcome')
        }
      },
      // 获取所有菜单
    async getMenuList() {
      const data = await this.$http.get('menus')
      if (data.data.meta.status !== 200) {
        return this.$message.error(data.data.meta.msg)
      }
      this.menulist = data.data.data
      console.log(data.data.data)
    },
    // 点击按钮切换菜单的折叠与展开
      toggleCollapse() {
        this.isCollapse = !this.isCollapse
        if (this.isCollapse === true) {
          this.isCollapse2 = '展开'
        } else {
          this.isCollapse2 = '折叠'
        }
      },
      // 保存链接的激活状态
      saveNavState(activePath) {
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      }
  },
  created() {
    this.getMenuList()
    // 判断是否是刚刚登陆进来，刚登入进来的话显示首页数据
    const path = window.sessionStorage.getItem('activePath')
    if (path !== null) {
      this.activePath = path
    } else {
      this.activePath = '/home'
    }
  }
}
</script>

<style lang="less" scoped>
.portrait{
  border-radius: 8px;
}
span{
  outline:none
}
.el-dropdown{
  margin-top: 4px;
}
.el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
    margin-top: 5px;
  }
.el-icon-arrow-down {
    font-size: 12px;
}
.home-container{
  height: 100%;
  .title_btn{
    display: flex;
    justify-content: flex-end;
    width: 100%;
  }
}
.el-header{
  background-color:rgb(48,65,86);
  color: white;
  font-size: 25px;
  .title_img{
    display: flex;
    align-items: center;
    span{
      margin-left: 15px;
    }
  }
}
.el-aside{
  background-color: rgb(45,60,80);
}
.el-main{
  background-color: rgb(239,241,244);
}
.iconfont{
  margin-right: 10px;
  margin-left: 3px;
}
 .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
.toggle-button{
  font-size: 20px;
  line-height: 50px;
  color: #fff;
  cursor:pointer;
  height: 50px;
  text-align: center;
  background: rgb(38,52,69)
}
</style>
